﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BobcatUtility;

namespace CNUtility
{
    /// <summary>
    /// Holds users preferences and other setup information. This class is a singleton
    /// so that the properties can be accessed anywhere in the application.
    /// 
    /// There are two singleton methods. One returns the single instance of this class.
    /// The other is a helper method that returns the PropertyBag that the instance holds. 
    /// 
    /// initialize
    /// ----------
    /// This function loads the preferences from a file and sets up defaults for the
    /// Notepad app. You may want to add extra defaults here.
    /// 
    /// </summary>
    public class UserPreferences
    {
        #region Initialization

        /// <summary>
        /// Initializes the user preferences. This loads them from a file and
        /// sets defaults for any properrties that weren't in the file.
        /// </summary>
        public void initialize()
        {
            m_PropertyBag.readFromFile(FILENAME);

            // We observe any changes to the property-bag, so that we can save it...
            m_PropertyBag.DataChangedEvent += onDataChanged;
        }

        #endregion

        #region Public properties

        /// <summary>
        /// Gets the PropertyBag we hold.
        /// </summary>
        public PropertyBag PropertyBag
        {
            get { return m_PropertyBag; }
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Singleton's getInstance() method.
        /// </summary>
        /// <returns></returns>
        public static UserPreferences getInstance()
        {
            if (m_Instance == null)
            {
                m_Instance = new UserPreferences();
            }
            return m_Instance;
        }

        /// <summary>
        /// Static helper method to return the PropertyBag held by the singleton instance.
        /// </summary>
        public static PropertyBag getPropertyBag()
        {
            return getInstance().m_PropertyBag;
        }

        #endregion

        #region Private functions

        /// <summary>
        /// Constructor is private as this class is a singleton.
        /// </summary>
        private UserPreferences()
        {
        }

        /// <summary>
        /// Called when the data in the property bag is updated. 
        /// </summary>
        private void onDataChanged(object sender, PropertyBag.Args e)
        {
            // We store the data in the file...
            m_PropertyBag.writeToFile(FILENAME);
        }

        #endregion

        #region Private data

        private const string FILENAME = "UserPreferences.xml";

        private static UserPreferences m_Instance = null;
        private PropertyBag m_PropertyBag = new PropertyBag();

        #endregion
    }
}
